package com.onlineauction.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.onlineauction.entities.User;
import com.onlineauction.helpers.Constants;

public class HomeServlet extends HttpServlet {

	private static final long serialVersionUID = 7160260169098319768L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
			ServletException {

		HttpSession session = request.getSession(true);
		String username = (String) session.getAttribute(Constants.USERNAME);

		// user not logged in
		if (username == null) {
			response.sendRedirect("/LoginAccount");
			return;
		}

		// user logged in
		User user = new User(username);
		request.setAttribute(Constants.USER, user);		
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("/home.jsp");
		requestDispatcher.forward(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}

}
